package com.sausageking.client.merchant.ui;

import java.util.Collection;

import javax.validation.ConstraintViolation;

import com.google.common.base.Strings;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.sausageking.shared.Merchant;
import com.sausageking.shared.Offer;
import com.sausageking.shared.Offer.TRIGGER;

public class MerchantSignupViewImpl extends Composite
    implements
      MerchantSignupView {

  private static MerchantSignupViewImplUiBinder uiBinder = GWT
      .create(MerchantSignupViewImplUiBinder.class);
  interface MerchantSignupViewImplUiBinder
      extends
        UiBinder<Widget, MerchantSignupViewImpl> {
  }

  @UiField
  TextBox searchText;
  @UiField
  Button searchButton;

  @UiField
  TextBox searchEmailBox;
  @UiField
  Button searchEmailButton;
  @UiField
  VerticalPanel linkPanel;
  @UiField
  TextBox photoUrlTextBox;

  @UiField
  Label idLabel;
  @UiField
  TextBox nameBox;
  @UiField
  TextArea descriptionArea;
  @UiField
  TextBox emailBox;
  @UiField
  TextBox websiteBox;
  @UiField
  TextBox phoneBox;
  @UiField
  TextBox streetBox;
  @UiField
  TextBox cityBox;
  @UiField
  TextBox zipBox;
  @UiField
  TextBox stateBox;
  @UiField
  Button submitButton;
  @UiField
  ListBox loyaltyOfferRewardType;
  @UiField
  TextBox loyaltyOfferValue;
  @UiField
  TextBox loyalltyOfferNumVisits;
  @UiField
  TextBox loyaltyROfferImageUrl;
  

  private Presenter presenter;

  @Inject
  public MerchantSignupViewImpl() {
    initWidget(uiBinder.createAndBindUi(this));
  }

  @UiHandler("submitButton")
  void onSubmitClick(ClickEvent e) {
    presenter.submit();
  }

  @UiHandler("searchButton")
  void onSearchClick(ClickEvent e) {
    presenter.search();
  }

  @UiHandler("searchEmailButton")
  void onSearchEmailClick(ClickEvent e) {
    presenter.searchByEmail();
  }

  @Override
  public void setPresenter(Presenter presenter) {
    this.presenter = presenter;
  }

  @Override
  public void setMerchant(Merchant merchant) {
    if (merchant.hasId()) {
      idLabel.setText(Long.toString(merchant.getId()));
      showStickerLink(true);
      
    } else {
      showStickerLink(false);
    }
    nameBox.setText(merchant.getName());
    descriptionArea.setText(merchant.getDescription());
    emailBox.setText(merchant.getEmail());
    phoneBox.setText(merchant.getPhone());
    websiteBox.setText(merchant.getWebsite());
    streetBox.setText(merchant.getStreet());
    cityBox.setText(merchant.getCity());
    stateBox.setText(merchant.getState());
    zipBox.setText(merchant.getZip());
    phoneBox.setText(merchant.getPhotoUrl());
    photoUrlTextBox.setText(merchant.getPhotoUrl());
    
    if (merchant.getOffer() != null) {
      Offer offer = merchant.getOffer();
      loyaltyOfferRewardType.setSelectedIndex(offer.getRewardType().ordinal());
      if (!Strings.isNullOrEmpty(offer.getRewardDescription())) {
        loyaltyOfferValue.setText(offer.getRewardDescription());
      } else {
        loyaltyOfferValue.setText(Double.toString(offer.getDiscountValue()));
      }
      loyalltyOfferNumVisits.setText(Integer.toString(offer.getCheckinCount()));
      loyaltyROfferImageUrl.setText(offer.getImageUrl());
    }

    if (merchant.getId() != null) {
      submitButton.setText("Update");
    } else {
      submitButton.setText("Insert");
    }
  }

  private void showStickerLink(boolean show) {
    linkPanel.clear();
    if (show) {
      Anchor stickerAnchor = new Anchor();
      stickerAnchor.setText("sticker");
      stickerAnchor.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
          presenter.goToStickerPlace();
        }
      });
      stickerAnchor.setText("Sticker");
      linkPanel.add(stickerAnchor);
    } 
  }

  @Override
  public Merchant getMerchant() {
    Merchant merchant = new Merchant();
    if (!Strings.isNullOrEmpty(idLabel.getText())) {
      merchant.setId(Long.valueOf(idLabel.getText()));
    }
    merchant.setName(nameBox.getText());
    merchant.setDescription(descriptionArea.getText());
    merchant.setEmail(emailBox.getText());
    merchant.setPhone(phoneBox.getText());
    merchant.setWebsite(websiteBox.getText());
    merchant.setStreet(streetBox.getText());
    merchant.setCity(cityBox.getText());
    merchant.setState(stateBox.getText());
    merchant.setZip(zipBox.getText());
    merchant.setPhotoUrl(photoUrlTextBox.getText());

    if (!Strings.isNullOrEmpty(loyaltyOfferValue.getText())) {
      Offer offer = new Offer();
      String rewardStr = loyaltyOfferRewardType.getValue(loyaltyOfferRewardType
          .getSelectedIndex());
      Offer.REWARD reward = Offer.REWARD.valueOf(rewardStr);
      offer.setRewardType(reward);
      offer.setTrigger(TRIGGER.EVERY_X_CHECK_INS);
      switch (reward) {
        case CUSTOMIZED :
          offer.setRewardDescription(loyaltyOfferValue.getText());
          break;
        case FLAT_DISCOUNT :
        case PERCENTAGE_DISCOUNT :
          offer.setDiscountValue(Double.valueOf(loyaltyOfferValue.getText()));
          break;
      }
      offer.setCheckinCount(Integer.valueOf(loyalltyOfferNumVisits.getText()));
      offer.setImageUrl(loyaltyROfferImageUrl.getText());
      merchant.setOffer(offer);
    }
    return merchant;
  }
  @Override
  public void setError(Collection<ConstraintViolation<Merchant>> errors) {
  }

  @Override
  public void clearError() {
  }

  @Override
  public String getSearchId() {
    return searchText.getText();
  }

  @Override
  public String getSearchEmail() {
    return searchEmailBox.getText();
  }

  @Override
  public void setSearchId(String id) {
    searchText.setText(id);
  }
}
